/*
 * test.c
 *
 *  Created on: May 13, 2016
 *      Author: root
 */

#include<stdlib.h>
#include<stdio.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include "test.h"
#include "../../util/log.h"
#include "../conf/dev_conf.h"
#include "../../app/gvar.h"
#include "../sqldb/db_helper.h"
#include "../tcp/tcp_recv_cmd.h"
#include "../tcp/tcp_send_cmd.h"
#include "../tcp/json_helper.h"



void test_set_notify_timer(){
    struct itimerval itv;
    itv.it_value.tv_sec = 10;    //timer start after 3 seconds later
    itv.it_value.tv_usec = 0;
    itv.it_interval.tv_sec = 10;
    itv.it_interval.tv_usec = 0;
    setitimer(ITIMER_REAL,&itv,NULL);
}

static int t_count = 0;
void test_notify_dev_sta(int m){
	t_count++;
	if(t_count >= 10000){
		t_count = 0;
	}
	Log("signal_handler -->>>t_count =  %d \n",t_count);

	if((t_count%2) == 0){
		DeviceState device_state;
		device_state.gpm = 1024;
		device_state.net_speed = 2904;
		device_state.user_count = 3;
		notify_device_state(device_state);
	}else{
		NetStatus net_status;
		net_status = NET_WIFIERROR;
		notify_net_status(net_status);
	}
}

void test_show_dev_conf(){
	Log("test_get_conf()---->>>>>dev_master_id = %d\n", g_dev_conf.dev_master_id);
	Log("test_get_conf()---->>>>>dev_wifi_ssid = %s\n", g_dev_conf.dev_wifi_ssid);
	Log("test_get_conf()---->>>>>dev_wifi_pwd = %s\n", g_dev_conf.dev_wifi_pwd);
	Log("test_get_conf()---->>>>>dev_mac = %s\n", g_dev_conf.dev_mac);
}

void test_set_dev_master_id(){
	LogStr("test_set_dev_master_id()\n");
	set_dev_master_id(6);

}

void test_set_dev_wifi_ssid(){
	char ssid[128] = "TP-Link_13988";
	char pwd[128] = "12345678";
//	set_dev_wifi_info(ssid, pwd);
	int ret = set_dev_wifi_ssid(ssid);
}

void test_get_conf(){
	int master_id = get_dev_master_id();
	Log("test_get_conf()---->>>>>master_id = %d\n", master_id);

	char ssid[64];
//	get_dev_wifi_ssid(&ssid);
//	Log("test_get_conf()---->>>>>ssid = %s\n", ssid);

	get_dev_wifi_conf(&g_dev_conf);
	test_show_dev_conf();
}


void test_get_ping_ip_list(){
	PingIp ip_list[20];
	int count = get_ping_ip_list(ip_list, 20);
	int i;
	for(i = 0; i < count; i++){
		Log("test_get_ping_ip_list()---->>>>>ip = %s", ip_list[i].host);
	}
}

void test_db_insert(){
	db_init_wifiinfoDB();
	WifiInfo wifilist[6];
	int i;
	for(i = 0; i < 6; i++){
		wifilist[i].wifi_id = 120;
		wifilist[i].wifi_area_id = 580021;
		wifilist[i].wifi_latitude = 11.234575;
		wifilist[i].wifi_longitude = 123.34455;
		Log("---db_init_wifiinfoDB()--->i = %d\n", i);
		char mac[32] ;
		sprintf(mac, "mac:23:e5:24:32:59:0%d", i);
		strcpy(wifilist[i].wifi_mac, mac);
		wifilist[i].wifi_matser_id = 112;
		strcpy(wifilist[i].wifi_ssid, "WL-Link_3324");
		strcpy(wifilist[i].wifi_password, "1246785");
	}
	Log("test_db_insert()---->>>>>wifi_mac = %s\n", wifilist[0].wifi_mac);
	db_insert_wifilist(wifilist, 6);
}


void test_db_select(){
	DBWifiInfo db_wifilist[10];
	int real_num = db_select_wifilist(db_wifilist, 5, 3);
	Log("---test_db_select()--->real_num = %d\n", real_num);
	int i;
	for(i = 0;i < real_num;i++){
		LogStr("t<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
		Log("test_db_select()---->>>>>db_id = %d\n", db_wifilist[i].db_id);
		Log("test_db_select()---->>>>>wifi_id = %d\n", db_wifilist[i].wifiinfo.wifi_id);
		Log("test_db_select()---->>>>>wifi_area_id = %d\n", db_wifilist[i].wifiinfo.wifi_area_id);
		Log("test_db_select()---->>>>>wifi_matser_id = %d\n", db_wifilist[i].wifiinfo.wifi_matser_id);
		Log("test_db_select()---->>>>>wifi_ssid = %s\n", db_wifilist[i].wifiinfo.wifi_ssid);
		Log("test_db_select()---->>>>>wifi_password = %s\n", db_wifilist[i].wifiinfo.wifi_password);
		Log("test_db_select()---->>>>>wifi_mac = %s\n", db_wifilist[i].wifiinfo.wifi_mac);
		Log("test_db_select()---->>>>>wifi_latitude = %f\n", db_wifilist[i].wifiinfo.wifi_latitude);
		Log("test_db_select()---->>>>>wifi_longitude = %f\n", db_wifilist[i].wifiinfo.wifi_longitude);
	}
}

void test_unbindDevice_json(){
	char *json_str = "{\"jsonrpc\":\"1.0\",\"method\":\"unbindDevice\",\"params\":{\"deviceMasterId\":10,\"deviceMac\":\"2e:34:45:3d:3d:3e\"},\"id\":6}";
//	json_str = get_json_str_of_rep_unbindDevice(1);
//	json_str = get_json_str_of_error_unbindDevice(ERR_CODE_UNBINDDEVICE_OF_COMMON_FAILED, ERR_MSG_UNBINDDEVICE_OF_COMMON_FAILED);
	ReqUnbindDevice req_unbindDevice;
	Log("json_str = %s\n", json_str);
	parse_req_for_unbindDevice(&req_unbindDevice, json_str);
	Log("id = %d\n", req_unbindDevice.req_base.id);
	Log("method = %s\n", req_unbindDevice.req_base.method);
	Log("dev_master_id = %d\n", req_unbindDevice.dev_master_id);
	Log("devr_mac = %s\n", req_unbindDevice.dev_mac);
}

void test_enum(){
	NetStatus netStatus = NET_NORMAL;
	Log("netStatus = %d\n", netStatus);
	int status = netStatus;
	Log("status = %d\n", status);
}

void test_getDeviceStatus_json(){
	char *json_str;


//	DeviceStatus dev_status;
//	dev_status.net_status = NET_NORMAL;
//	dev_status.work_status = WORK_NORMAL;
//	json_str = get_json_str_of_rep_getDeviceStatus(dev_status);
//	Log("json_str = %s\n", json_str);
//	free(json_str);
//	json_str = NULL;
//
//	json_str = get_json_str_of_error_getDeviceStatus(ERR_CODE_GETDEVICESTATUS_OF_COMMON_FAILED, ERR_MSG_GETDEVICESTATUS_OF_COMMON_FAILED);
//	Log("json_str = %s\n", json_str);
//	free(json_str);
//	json_str = NULL;

//	json_str = "{\"jsonrpc\":\"1.0\",\"method\":\"getDeviceStatus\",\"result\":{\"workStatus\":1,\"netStatus\":1},\"id\":7}";
//	ReqGetDeviceStatus req_getDeviceStatus;
//	parse_req_for_getDeviceStatus(&req_getDeviceStatus, json_str);
//	Log("id = %d\n", req_getDeviceStatus.req_base.id);
//	Log("method = %s\n", req_getDeviceStatus.req_base.method);

	NetStatus net_status = NET_NORMAL;
	char* json_send_str = get_json_str_of_notify_net_status(net_status);
	Log("json_send_str = %s\n", json_send_str);
	free(json_str);
	json_str = NULL;
}

void test_fun(){

//	init_dev_config();
//	db_init_wifiinfoDB();
//
//	start_tcp_recv_cmd_server();



//	test_unbindDevice_json();


//	start_tcp_send_cmd_server();
//	signal(SIGALRM, test_notify_dev_sta);
//	test_set_notify_timer();
//	while(1);

//	test_set_dev_master_id();
//	test_get_conf();


//	test_show_dev_conf();
//
//	test_set_dev_wifi_ssid();
//	test_show_dev_conf();

//	test_get_ping_ip_list();
//	test_db_insert();


//	test_db_select();
//	db_delete_wifiinfo_by_mac("mac:23:e5:24:32:59:00");
//	db_delete_wifiinfo_by_id(5);
//	int ret = db_is_wifiinfo_exist("2e3d3d6d7d7d");

//	test_enum();
//	test_getDeviceStatus_json();
}
